package com.gitee.mrj.aiboot.core.sf.generators.ui.widget;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gitee.mrj.aiboot.annotation.config.NzButtonType;
import com.gitee.mrj.aiboot.annotation.config.sf.ui.widget.SFArrayWidget;
import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import com.gitee.mrj.aiboot.core.sf.generators.UIForm;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class SFArrayWidgetGenerator implements FormDefinitionGenerator {
    @Override
    public void generate(UIForm root, Field field, UIForm node) {
        SFArrayWidget arrayWidget = AnnotatedElementUtils.findMergedAnnotation(field, SFArrayWidget.class);
        generate(node, arrayWidget);
    }

    @Override
    public void generate(UIForm node, Class clazz) {
        SFArrayWidget arrayWidget = AnnotatedElementUtils.findMergedAnnotation(clazz, SFArrayWidget.class);
        generate(node, arrayWidget);
    }

    private void generate(UIForm node, SFArrayWidget arrayWidget) {
        ObjectNode ui = node.getUi();

        putString(ui, "addTitle", arrayWidget.addTitle());
        putString(ui, "removeTitle", arrayWidget.removeTitle());
        ui.put("removable", arrayWidget.removable());
        NzButtonType nzButtonType = arrayWidget.addType();
        ui.put("addType", nzButtonType.value());
    }


    @Override
    public Class[] supports() {
        return new Class[]{SFArrayWidget.class};
    }
}
